{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from efficient_apriori import apriori"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: {('啤酒',): 3, ('尿布',): 5, ('牛奶',): 4, ('面包',): 4}, 2: {('啤酒', '尿布'): 3, ('尿布', '牛奶'): 4, ('尿布', '面包'): 4, ('牛奶', '面包'): 3}, 3: {('尿布', '牛奶', '面包'): 3}}\n",
      "[{啤酒} -> {尿布}, {牛奶} -> {尿布}, {面包} -> {尿布}, {牛奶, 面包} -> {尿布}]\n"
     ]
    }
   ],
   "source": [
    "# 设置数据集\n",
    "data = [['牛奶','面包','尿布'],\n",
    "           ['可乐','面包', '尿布', '啤酒'],\n",
    "           ['牛奶','尿布', '啤酒', '鸡蛋'],\n",
    "           ['面包', '牛奶', '尿布', '啤酒'],\n",
    "           ['面包', '牛奶', '尿布', '可乐']]\n",
    "# 挖掘频繁项集和频繁规则，efficient_apriori不开率item顺序\n",
    "# min_support最小支持度，min_confidence最小置信度\n",
    "itemsets, rules = apriori(data, min_support=0.5, min_confidence=1)\n",
    "print(itemsets) # 频繁项集\n",
    "print(rules) # 频繁规则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[' 郭涛 ', ' 刘桦 ', ' 连晋 ', ' 黄渤 ', ' 徐峥 ', ' 优恵 ', ' 罗兰  ', ' 王迅'],\n",
       " [' 徐峥 ', ' 黄渤 ', ' 余男 ', ' 多布杰 ', ' 王双宝 ', ' 巴多 ', ' 杨新鸣 ', ' 郭虹'],\n",
       " [' 黄渤 ', ' 戎祥 ', ' 九孔 ', ' 徐峥 ', ' 王双宝 ', ' 巴多 ', ' 董立范 ', ' 高捷'],\n",
       " [' 黄渤 ', ' 徐峥 ', ' 袁泉 ', ' 周冬雨 ', ' 陶慧 ', ' 岳小军 ', ' 沈腾 ', ' 张俪'],\n",
       " [' 雷佳音 ', ' 陶虹 ', ' 程媛媛 ', ' 山崎敬一 ', ' 郭涛 ', ' 范伟 ', ' 孙淳 ', ' 刘桦'],\n",
       " [' 李强'],\n",
       " [' 张曦 ', ' 黄渤 ', ' 王玮 ', ' 聂鑫 ', ' 王迅 ', ' 岳小军'],\n",
       " [' 巴德玛'],\n",
       " []]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./宁浩.csv', header=0, names=range(1,9))\n",
    "# 将数据转化为列表形式输出\n",
    "data = []\n",
    "for index,row in df.iterrows():\n",
    "    actor = []\n",
    "    actor = row.tolist()\n",
    "    actor = [i for i in actor if not pd.isnull(i)]\n",
    "    data.append(actor)\n",
    "#     for r in row:\n",
    "#         if not pd.isnull(r):\n",
    "#             actor.append(r)\n",
    "#     data.append(actor)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: {(' 徐峥 ',): 4, (' 黄渤 ',): 5}, 2: {(' 徐峥 ', ' 黄渤 '): 4}}\n",
      "[{ 徐峥 } -> { 黄渤 }]\n"
     ]
    }
   ],
   "source": [
    "itemsets, rules = apriori(data, min_support=0.5, min_confidence=1)\n",
    "print(itemsets)\n",
    "print(rules)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
